import 'package:flutter/material.dart';
import 'package:my_app/pages/searching_content.dart';
import 'package:my_app/tools/http_server.dart';

class RecordItems extends StatefulWidget {
  const RecordItems({Key? key}) : super(key: key);

  @override
  _RecordItemsState createState() => _RecordItemsState();
}

class _RecordItemsState extends State<RecordItems> {
  List _list = [];

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: HttpServer.getHot(),
      builder: (BuildContext context, AsyncSnapshot<List> snapshot){
        if(snapshot.connectionState == ConnectionState.done){
          if(snapshot.hasError){
            return Text("错误");
          }else{
            var data = snapshot.data;
            print("data: $data");
            List<Map<String, dynamic>> tiles = (data as List).cast();
            return Padding(
              padding: EdgeInsets.all(15),
              child: Container(
                margin: EdgeInsets.only(top: 0.0, left: 0.0),
                constraints: BoxConstraints.tightFor(width: 300.0, height: 300.0),
                decoration: BoxDecoration(
                    gradient: RadialGradient(
                        colors: [Colors.blueGrey.shade600,Colors.blueGrey.shade800],
                        center: Alignment.topLeft,
                        radius: 1),
                    // gradient 适用于渐变色的控制的
                    borderRadius: BorderRadius.all(Radius.circular(15)),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.black45,
                          offset: Offset(3.0, 3.0),
                          blurRadius: 4.0)
                    ]),
                width: 300,
                child: DataTable(
                    columns: [
                      DataColumn(
                          label: Text(
                            "次数",
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.w700,
                              color: const Color(0xffa68a64),
                            ),
                          )),
                      DataColumn(
                        label: Text(
                          "名称",
                          style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.w700,
                            color: const Color(0xffa68a64),
                          ),
                        ),
                        numeric: true,
                      )
                    ],
                    rows: tiles
                        .map(
                          (e) => DataRow(
                        cells: [
                          DataCell(Text(
                            e["count"].toString(),
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.w400,
                              color: const Color(0xffc2c5aa),
                            ),
                          )),
                          DataCell(
                            Text(
                              e["sname"].toString(),
                              style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.w400,
                                color: const Color(0xffc2c5aa),
                              ),
                            ),
                            onTap: (){
                              Navigator.of(context).push(
                                MaterialPageRoute(
                                  builder: (context){
                                    return Scaffold(
                                      appBar: AppBar(title: Text("查找"),),
                                      body: SearchingContent(e["sname"]),
                                    );
                                  }
                                )
                              );
                            }
                          ),
                        ],
                      ),
                    )
                        .toList()),
              ),
            );
            // return ListView.builder(
            //     itemCount: tiles.length,
            //     shrinkWrap: true,
            //     itemBuilder: (BuildContext context, int index) {
            //       return ListTile(
            //         title:  Text('${(tiles)[index]["sname"]}'),
            //         subtitle: Text('${(tiles)[index]["count"]}'),
            //       );
            //     }
            // );
          }
        } else{
          return Align(
            child: CircularProgressIndicator(),
            alignment: Alignment.center,
          );
        }
      },
    );

  }
}
